﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.DAL;
using MINERP.MODEL.FoundationModel;
using MINERP.COM;
using System.Transactions;

namespace MINERP.BLL.Statistics
{
    public class OutReturnBLL
    {
        newSampleYPDBEntities newEntity = new newSampleYPDBEntities();
        public OutReturnMain_Info GetOneReturn(string inProductBarcode)
        {
            OutReturnMain newReturn = newEntity.OutReturnMain.FirstOrDefault(p => p.OrderProductCode == inProductBarcode);
            return ConvetOutReturnMainToInfo(newReturn);
        }

        /// <summary>
        /// 新添加时准备数据
        /// </summary>
        /// <param name="inProductBarCode"></param>
        /// <returns></returns>
        public OutRentunView GetViewForCreate(string inProductBarCode)
        {
            OutRentunView rtnView = new OutRentunView();
            int intNormalProduct = (int)OrderInfoType.Normal;
            orderInfo_BK orderProduct = newEntity.orderInfo_BK.FirstOrDefault(p => p.OrderProductCode == inProductBarCode && p.ProductType == intNormalProduct);
            if (orderProduct == null)
            {
                return null;
            }
            if (!orderProduct.Tb_OrderBag_BKReference.IsLoaded)
            {
                orderProduct.Tb_OrderBag_BKReference.Load();
            }
            Tb_OrderBag_BK orderBag = orderProduct.Tb_OrderBag_BK;
            if (!orderBag.ShopReference.IsLoaded)
            {
                orderBag.ShopReference.Load();
            }
            OutReturnMain_Info mainInfo = new OutReturnMain_Info();
            mainInfo.Shopid = orderBag.Shop.ShopId;
            mainInfo.ShopName = orderBag.Shop.shopName;
            mainInfo.ProductWidth = orderProduct.Width;
            mainInfo.ProductNum = orderProduct.productNum;
            mainInfo.ProductHeight = orderProduct.Height;
            mainInfo.ProductCompleteTime = orderProduct.CompleteTime;
            mainInfo.OrderTime = orderBag.OrderTime;
            mainInfo.OrderProductName = orderProduct.OrderProductName;
            mainInfo.OrderProductCode = orderProduct.OrderProductCode;
            mainInfo.OrderBagName = orderBag.BagName;
            mainInfo.OrderBagId = orderBag.OrderBagId;
            mainInfo.CtmName = orderProduct.CtmName;

            List<OutReturnPhoto_Info> listPhotoInfo = new List<OutReturnPhoto_Info>();
            if (!orderProduct.photoInfo_BK.IsLoaded)
            {
                orderProduct.photoInfo_BK.Load();
            }
            List<photoInfo_BK> listPhoto = orderProduct.photoInfo_BK.ToList();
            foreach (photoInfo_BK photo in listPhoto)
            {
                OutReturnPhoto_Info returnInfo = new OutReturnPhoto_Info()
                {
                    Height = photo.Height,
                    IsSelect = false,
                    PhotoId = photo.photoId,
                    PhotoBarcode = photo.PhotoBarCode,
                    PhotoName = photo.photoName,
                    Weight = photo.Width,
                    PhotoPath = photo.picPath

                };
                listPhotoInfo.Add(returnInfo);
            }
            rtnView.mainInfo = mainInfo;
            rtnView.PhotoListInfo = listPhotoInfo;
            return rtnView;
        }
        /// <summary>
        /// 更新时准备数据
        /// </summary>
        /// <param name="inProductBarCode"></param>
        /// <returns></returns>
        public OutRentunView GetViewForUpdate(string inProductBarCode)
        {
            OutRentunView rtnView = new OutRentunView();
            List<OutReturnPhoto_Info> ListPhotoInfo = new List<OutReturnPhoto_Info>();

            OutReturnMain outReturnMain = newEntity.OutReturnMain.FirstOrDefault(p => p.OrderProductCode == inProductBarCode);

            if (outReturnMain == null)
            {
                return null;
            }
            OutReturnMain_Info info = ConvetOutReturnMainToInfo(outReturnMain);
            info.ShopName = newEntity.Shop.FirstOrDefault(p => p.ShopId == info.Shopid).shopName;
            if (!outReturnMain.OutReturnPhoto.IsLoaded)
            {
                outReturnMain.OutReturnPhoto.Load();
            }
            List<OutReturnPhoto> ListPhoto = outReturnMain.OutReturnPhoto.ToList();
            foreach (OutReturnPhoto photo in ListPhoto)
            {
                ListPhotoInfo.Add(ConvertOutReturnPhoto(photo));
            }
            rtnView.mainInfo = info;
            rtnView.PhotoListInfo = ListPhotoInfo;
            return rtnView;

        }
        /// <summary>
        /// 获得产品的流程列表
        /// </summary>
        /// <param name="inProductid"></param>
        /// <returns></returns>
        public List<Tb_Tech_Info> GetProductTechList(int inProductid)
        {
            List<WorkOrderFlowList_BK> workList = newEntity.WorkOrderFlowList_BK.Where(p => p.orderInfo_BK.OrderProductId == inProductid).OrderBy(t => t.WorkOrder).ToList();
            List<Tb_Tech_Info> ListTech = new List<Tb_Tech_Info>();
            foreach (WorkOrderFlowList_BK flow in workList)
            {
                if (!flow.tb_TechReference.IsLoaded)
                {
                    flow.tb_TechReference.Load();
                }
                tb_Tech tech = flow.tb_Tech;
                Tb_Tech_Info info = new Tb_Tech_Info() { TechId = tech.TechId, TechName = tech.TechName };
                ListTech.Add(info);
            }
            return ListTech;
        }
        /// <summary>
        /// 获得产品的流程列表1ge
        /// </summary>
        /// <param name="inProductid"></param>
        /// <returns></returns>
        public Tb_Tech_Info GetProductTechByProductIdAndTechid(int inProductid, int inTechid)
        {
            WorkOrderFlowList_BK workList = newEntity.WorkOrderFlowList_BK.FirstOrDefault(p => p.orderInfo_BK.OrderProductId == inProductid && p.tb_Tech.TechId == inTechid);


            if (!workList.tb_TechReference.IsLoaded)
            {
                workList.tb_TechReference.Load();
            }
            tb_Tech tech = workList.tb_Tech;
            Tb_Tech_Info info = new Tb_Tech_Info() { TechId = tech.TechId, TechName = tech.TechName };


            return info;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="inProductid"></param>
        /// <param name="intechid"></param>
        /// <returns></returns>
        public List<User_Info> GetUserByProductIdAndTechId(int inProductid, int intechid)
        {
            List<User_Info> list = new List<User_Info>();
            WorkOrderFlowList_BK workFlowList = newEntity.WorkOrderFlowList_BK.FirstOrDefault(p => p.orderInfo_BK.OrderProductId == inProductid && p.tb_Tech.TechId == intechid);
            users use = newEntity.users.FirstOrDefault(p => p.userID == workFlowList.UserId);
            User_Info info = new User_Info() { UserID = use.userID, UserName = use.UserName };
            list.Add(info);
            return list;

        }

        /// <summary>
        /// 获取节点操作人
        /// </summary>
        /// <param name="inTechid"></param>
        /// <returns></returns>
        public List<User_Info> GetUsersByTech(int inTechid)
        {
            List<User_Info> rtnUsers = new List<User_Info>();
            tb_Tech tech = newEntity.tb_Tech.Where(p => p.TechId == inTechid).FirstOrDefault();
            tech.users.Load();


            List<users> listUser = tech.users.ToList();
            foreach (users list in listUser)
            {
                User_Info info = new User_Info();
                info = new User_Info() { UserID = list.userID, UserName = list.UserName };
                rtnUsers.Add(info);

            }
            return rtnUsers;
        }
        /// <summary>
        /// 新添加一条外部反单记录
        /// </summary>
        /// <param name="inView"></param>
        public void InsetRecord(OutRentunView inView)
        {
            System.Transactions.TransactionOptions options = new TransactionOptions();
            options.Timeout = new TimeSpan(0, 0, 600);
            options.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
            int orderproductid = Convert.ToInt32(inView.mainInfo.OrderProductCode.TrimStart('2').TrimStart('0'));
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, options))
            {
                // var aaa=(DateTime)newEntity.WorkOrderFlowList_BK.FirstOrDefault(P => P.tb_Tech.TechId == inView.mainInfo.Techid && P.UserId == inView.mainInfo.ResponUser && P.orderInfo_BK.OrderProductId == orderproductid).CompletedTime;
                OutReturnMain inMain = new OutReturnMain()
                {
                    CreateTime = inView.mainInfo.CreateTime,
                    CreateUser = inView.mainInfo.CreateUser,
                    CtmName = inView.mainInfo.CtmName,
                    OrderBagId = inView.mainInfo.OrderBagId,
                    OrderBagName = inView.mainInfo.OrderBagName,
                    OrderProductCode = inView.mainInfo.OrderProductCode,
                    OrderProductName = inView.mainInfo.OrderProductName,
                    OrderTime = inView.mainInfo.OrderTime,
                    ProductCompleteTime = (DateTime)newEntity.WorkOrderFlowList_BK.FirstOrDefault(P => P.tb_Tech.TechId == inView.mainInfo.Techid && P.UserId == inView.mainInfo.ResponUser && P.orderInfo_BK.OrderProductId == orderproductid).CompletedTime,
                    ProductHeight = inView.mainInfo.ProductHeight,
                    ProductNum = inView.mainInfo.ProductNum,
                    ProductWidth = inView.mainInfo.ProductWidth,
                    Remark = inView.mainInfo.Remark,
                    ResponUser = inView.mainInfo.ResponUser,
                    ReturnReason = inView.mainInfo.ReturnReason,
                    Shopid = inView.mainInfo.Shopid,
                    Techid = inView.mainInfo.Techid
                };
                List<OutReturnPhoto> PhotoList = new List<OutReturnPhoto>();
                foreach (OutReturnPhoto_Info info in inView.PhotoListInfo)
                {
                    OutReturnPhoto photo = new OutReturnPhoto();
                    photo.Height = info.Height;
                    photo.IsSelect = info.IsSelect;
                    photo.OutReturnMain = inMain;

                    photo.photoBarcode = info.PhotoBarcode;
                    photo.PhotoId = info.PhotoId;
                    photo.PhotoName = info.PhotoName;
                    photo.Weight = info.Weight;
                    photo.PhotoPath = info.PhotoPath;
                }
                newEntity.AddToOutReturnMain(inMain);
                newEntity.SaveChanges();
                scope.Complete();
            }

        }
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="inView"></param>
        public void UpdateOutReturn(OutRentunView inView)
        {
            try
            {
                OutReturnMain main = newEntity.OutReturnMain.FirstOrDefault(p => p.OutReturnMainId == inView.mainInfo.OutReturnMainId);

                //main.OutReturnMainId = inView.mainInfo.OutReturnMainId;
                main.CreateTime = inView.mainInfo.CreateTime;
                main.CtmName = inView.mainInfo.CtmName;
                main.CreateUser = inView.mainInfo.CreateUser;
                main.OrderBagId = inView.mainInfo.OrderBagId;
                main.OrderBagName = inView.mainInfo.OrderBagName;
                main.OrderProductCode = inView.mainInfo.OrderProductCode;
                main.OrderProductName = inView.mainInfo.OrderProductName;
                main.OrderTime = inView.mainInfo.OrderTime;
                main.ProductCompleteTime = inView.mainInfo.ProductCompleteTime;
                main.ProductHeight = inView.mainInfo.ProductHeight;
                main.ProductNum = inView.mainInfo.ProductNum;
                main.ProductWidth = inView.mainInfo.ProductWidth;
                main.Remark = inView.mainInfo.Remark;
                main.ResponUser = inView.mainInfo.ResponUser;
                main.ReturnReason = inView.mainInfo.ReturnReason;
                main.Shopid = inView.mainInfo.Shopid;
                main.Techid = inView.mainInfo.Techid;
                if (!main.OutReturnPhoto.IsLoaded)
                {
                    main.OutReturnPhoto.Load();
                }
                List<OutReturnPhoto> listPhoto = main.OutReturnPhoto.ToList();
                foreach (OutReturnPhoto photo in listPhoto)
                {
                    if (inView.PhotoListInfo.FirstOrDefault(p => p.OutReturnPhotoId == photo.OutReturnPhotoId).IsSelect)
                    {
                        photo.IsSelect = true;
                    }
                    else
                    {
                        photo.IsSelect = false;
                    }
                }
                newEntity.SaveChanges();
            }
            catch (Exception ex)
            {
                throw;
            }


        }
        public void DeleteRecord(int inMainID)
        {
            OutReturnMain main = newEntity.OutReturnMain.FirstOrDefault(p => p.OutReturnMainId == inMainID);
            newEntity.DeleteObject(main);
            newEntity.SaveChanges();
        }

        private OutReturnMain_Info ConvetOutReturnMainToInfo(OutReturnMain newReturn)
        {
            OutReturnMain_Info info = new OutReturnMain_Info()
            {
                CreateTime = newReturn.CreateTime,
                CtmName = newReturn.CtmName,
                CreateUser = newReturn.CreateUser,
                OrderBagId = newReturn.OrderBagId,
                OrderBagName = newReturn.OrderBagName,
                OrderProductCode = newReturn.OrderProductCode,
                OrderProductName = newReturn.OrderProductName,
                OrderTime = newReturn.OrderTime,
                OutReturnMainId = newReturn.OutReturnMainId,
                ProductCompleteTime = newReturn.ProductCompleteTime,
                ProductHeight = newReturn.ProductHeight,
                ProductNum = newReturn.ProductNum,
                ProductWidth = newReturn.ProductWidth,
                Remark = newReturn.Remark,
                ResponUser = newReturn.ResponUser,
                ReturnReason = newReturn.ReturnReason,
                Shopid = newReturn.Shopid,
                Techid = newReturn.Techid
            };
            return info;
        }

        private OutReturnPhoto_Info ConvertOutReturnPhoto(OutReturnPhoto inphoto)
        {
            OutReturnPhoto_Info info = new OutReturnPhoto_Info()
            {
                Weight = inphoto.Weight,
                PhotoName = inphoto.PhotoName,
                PhotoId = inphoto.PhotoId,
                PhotoBarcode = inphoto.photoBarcode,
                OutReturnPhotoId = inphoto.OutReturnPhotoId,
                IsSelect = inphoto.IsSelect,
                Height = inphoto.Height

            };
            return info;
        }
        public int GetProductIdByPhoto(int inphotoid)
        {

            photoInfo_BK photo = newEntity.photoInfo_BK.FirstOrDefault(p => p.photoId == inphotoid);
            if (photo == null)
            {
                return 0;
            }
            photo.orderInfo_BKReference.Load();
            return photo.orderInfo_BK.OrderProductId;


        }

    }


}
